package flc.ast.fragment;

import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.calculator.ci.model.CiCalculator;
import com.stark.calculator.ci.model.CiDetailRet;
import com.stark.calculator.ci.model.CiDurationUnit;
import com.stark.calculator.ci.model.CiEachDetail;
import com.stark.calculator.ci.model.CiErr;
import com.stark.calculator.ci.model.CiMethod;
import com.stark.calculator.ci.model.CiProject;
import com.stark.calculator.ci.model.CiRate;
import com.stark.calculator.tax.model.TaxCalRetBean;
import com.stark.calculator.tax.util.IncomeType;
import com.stark.calculator.tax.util.TaxUtil;

import java.util.List;

import flc.ast.activity.CiDetailActivity;
import flc.ast.R;
import flc.ast.activity.IncomeKind2RetActivity;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.databinding.FragmentIncomeKind3Binding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.Str2NumUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private CiCalculator mCiCalculator = new CiCalculator();
    private CiDetailRet mCiDetailRet;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.tvCalIncome.setOnClickListener(this);
        mDataBinding.tvCalPrincipal.setOnClickListener(this);
        mDataBinding.tvRateYear.setOnClickListener(this);
        mDataBinding.tvRateMonth.setOnClickListener(this);
        mDataBinding.tvRateDay.setOnClickListener(this);
        mDataBinding.tvCycleYear.setOnClickListener(this);
        mDataBinding.tvCycleMonth.setOnClickListener(this);
        mDataBinding.tvCycleDay.setOnClickListener(this);
        mDataBinding.tvWayYear.setOnClickListener(this);
        mDataBinding.tvWayMonth.setOnClickListener(this);
        mDataBinding.tvWayDay.setOnClickListener(this);
        mDataBinding.ivStartCal.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCalIncome:
                clearSelection();
                mDataBinding.tvAmountType.setText(getString(R.string.fuli_text));
                mDataBinding.tvCalIncome.setBackgroundResource(R.drawable.ak1);
                mDataBinding.tvCalIncome.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvCalIncome.setTextColor(Color.WHITE);
                mCiCalculator.setCiProject(CiProject.CAL_INCOME);
                break;
            case R.id.tvCalPrincipal:
                clearSelection();
                mDataBinding.tvAmountType.setText(getString(R.string.fuli_text2));
                mDataBinding.tvCalPrincipal.setBackgroundResource(R.drawable.ak3);
                mDataBinding.tvCalPrincipal.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvCalPrincipal.setTextColor(Color.WHITE);
                mCiCalculator.setCiProject(CiProject.CAL_PRINCIPAL);
                break;
            case R.id.tvRateYear:
                clearSelection1();
                mDataBinding.tvRateYear.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvRateYear.setTextColor(Color.WHITE);
                mCiCalculator.setCiRate(CiRate.YEAR);
                break;
            case R.id.tvRateMonth:
                clearSelection1();
                mDataBinding.tvRateMonth.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvRateMonth.setTextColor(Color.WHITE);
                mCiCalculator.setCiRate(CiRate.MONTH);
                break;
            case R.id.tvRateDay:
                clearSelection1();
                mDataBinding.tvRateDay.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvRateDay.setTextColor(Color.WHITE);
                mCiCalculator.setCiRate(CiRate.DAY);
                break;
            case R.id.tvCycleYear:
                clearSelection2();
                mDataBinding.tvCycleYear.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvCycleYear.setTextColor(Color.WHITE);
                mCiCalculator.setCiDurationUnit(CiDurationUnit.YEAR);
                break;
            case R.id.tvCycleMonth:
                clearSelection2();
                mDataBinding.tvCycleMonth.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvCycleMonth.setTextColor(Color.WHITE);
                mCiCalculator.setCiDurationUnit(CiDurationUnit.MONTH);
                break;
            case R.id.tvCycleDay:
                clearSelection2();
                mDataBinding.tvCycleDay.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvCycleDay.setTextColor(Color.WHITE);
                mCiCalculator.setCiDurationUnit(CiDurationUnit.DAY);
                break;
            case R.id.tvWayYear:
                clearSelection3();
                mDataBinding.tvWayYear.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvWayYear.setTextColor(Color.WHITE);
                mCiCalculator.setCiMethod(CiMethod.YEAR);
                break;
            case R.id.tvWayMonth:
                clearSelection3();
                mDataBinding.tvWayMonth.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvWayMonth.setTextColor(Color.WHITE);
                mCiCalculator.setCiMethod(CiMethod.MONTH);
                break;
            case R.id.tvWayDay:
                clearSelection3();
                mDataBinding.tvWayDay.setBackgroundResource(R.drawable.nianyueri1);
                mDataBinding.tvWayDay.setTextColor(Color.WHITE);
                mCiCalculator.setCiMethod(CiMethod.DAY);
                break;
            case R.id.ivStartCal:
                //金额
                String strAmount = mDataBinding.etPrincipal.getText().toString().trim();
                if (strAmount.isEmpty()) {
                    mDataBinding.etPrincipal.requestFocus();
                    ToastUtils.showShort(getString(R.string.please_input_money));
                    return;
                }
                double amount = Double.parseDouble(strAmount);
                mCiCalculator.setAmount(amount);
                //利率
                String strRate = mDataBinding.etInterestRate.getText().toString().trim();
                if (strRate.isEmpty()) {
                    mDataBinding.etInterestRate.requestFocus();
                    ToastUtils.showShort(getString(R.string.please_input_interest_rate));
                    return;
                }
                float rate = Float.parseFloat(strRate);
                rate = rate / 100;
                mCiCalculator.setRate(rate);
                //周期
                String strDuration = mDataBinding.etCycle.getText().toString().trim();
                if (strDuration.isEmpty()) {
                    mDataBinding.etCycle.requestFocus();
                    ToastUtils.showShort(getString(R.string.please_input_cycle));
                    return;
                }
                int duration = Integer.parseInt(strDuration);
                mCiCalculator.setDuration(duration);
                mCiCalculator.cal(new CiCalculator.IListener() {
                    @Override
                    public void onErr(CiErr err) {
                        ToastUtils.showShort(err.getMsg());
                    }

                    @Override
                    public void onResult(double amount, @NonNull List<CiEachDetail> ciEachDetailList) {
                        mCiDetailRet = new CiDetailRet();
                        mCiDetailRet.ciProject = mCiCalculator.getCiProject();
                        mCiDetailRet.ciMethod = mCiCalculator.getCiMethod();
                        mCiDetailRet.amount = mCiCalculator.getAmount();
                        mCiDetailRet.ciRate = mCiCalculator.getCiRate();
                        mCiDetailRet.rate = mCiCalculator.getRate();
                        mCiDetailRet.ciDurationUnit = mCiCalculator.getCiDurationUnit();
                        mCiDetailRet.duration = mCiCalculator.getDuration();
                        mCiDetailRet.detailList = ciEachDetailList;
                        CiDetailActivity.sCiDetailRet = mCiDetailRet;
                        startActivity(CiDetailActivity.class);
                    }
                });
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void clearSelection() {
        mDataBinding.tvCalPrincipal.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvCalPrincipal.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvCalPrincipal.setBackground(null);
        mDataBinding.tvCalIncome.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvCalIncome.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvCalIncome.setBackground(null);
        mDataBinding.etCycle.setText("");
        mDataBinding.etCycle.clearFocus();
        mDataBinding.etPrincipal.setText("");
        mDataBinding.etPrincipal.clearFocus();
        mDataBinding.etInterestRate.setText("");
        mDataBinding.etInterestRate.clearFocus();
    }

    private void clearSelection1() {
        mDataBinding.tvRateYear.setBackground(null);
        mDataBinding.tvRateYear.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvRateMonth.setBackground(null);
        mDataBinding.tvRateMonth.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvRateDay.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvRateDay.setBackground(null);
    }

    private void clearSelection2() {
        mDataBinding.tvCycleYear.setBackground(null);
        mDataBinding.tvCycleYear.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvCycleMonth.setBackground(null);
        mDataBinding.tvCycleMonth.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvCycleDay.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvCycleDay.setBackground(null);
    }

    private void clearSelection3() {
        mDataBinding.tvWayYear.setBackground(null);
        mDataBinding.tvWayYear.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvWayMonth.setBackground(null);
        mDataBinding.tvWayMonth.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvWayDay.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvWayDay.setBackground(null);
    }
}